package com.cn.shiro_demo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.cn.shiro_demo.mapper.*;
import com.cn.shiro_demo.model.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Log4j2
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    RoleAndPermisMapper roleAndPermisMapper;
    @Autowired
    UserAndRoleMapper userAndRoleMapper;

    @Override
    public Boolean login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(user)) {
            return false;
        }
        log.info("当前登录用户：{}", user.toString());
        if (user.getPassword().equals(password)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public User findByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return userMapper.selectOne(queryWrapper);

    }

    @Override
    public List<Role> findRolesByUserId(Long userId) {
        QueryWrapper<UserAndRole> userAndRoleQueryWrapper = new QueryWrapper<>();
        userAndRoleQueryWrapper.eq("user_id", userId);
        List<UserAndRole> userAndRoleList = userAndRoleMapper.selectList(userAndRoleQueryWrapper);

        List<Role> list = new ArrayList<>();
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        for (UserAndRole userAndRole : userAndRoleList) {
            Long roleId = userAndRole.getRoleId();
            roleQueryWrapper.eq("id", roleId);
            Role role = roleMapper.selectById(roleId);
            list.add(role);
        }
        return list;
    }

    @Override
    public List<String> findPermissionsByRoleId(Long roleId) {
        QueryWrapper<RoleAndPermis> roleAndPermisQueryWrapper = new QueryWrapper<>();
        roleAndPermisQueryWrapper.eq("role_id", roleId);
        List<RoleAndPermis> roleAndPermisList = roleAndPermisMapper.selectList(roleAndPermisQueryWrapper);

        List<String> list = new ArrayList<>();
        QueryWrapper<Permission> permissionQueryWrapper = new QueryWrapper<>();
        for (RoleAndPermis roleAndPermis : roleAndPermisList) {
            Long permisId = roleAndPermis.getPermisId();
            permissionQueryWrapper.eq("id", permisId);
            String name = permissionMapper.selectById(permisId).getName();
            list.add(name);
        }
        return list;
    }

//    public List<Role> findRoles(Long userId) {
//        this.f
//    }
}
